﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CommonLibrary.DataModel.WorkFlow;
using System.Data.SqlClient;
using CommonLibrary.DB;
using System.Data;
using CommonLibrary.DataModel.Form;

namespace ServiceApplication.Dao.WorkFlow
{
    public class WfDao
    {
        #region 基本

        public int AddEntity(WfEntity wfEntity)
        {
            List<SqlParameter> parameterList = new List<SqlParameter>();
            parameterList.Add(new SqlParameter("@wftemplate_id", wfEntity.wftemplate_id));
            parameterList.Add(new SqlParameter("@form_id", wfEntity.form_id));
            parameterList.Add(new SqlParameter("@wftype_id", wfEntity.wftype_id));
            parameterList.Add(new SqlParameter("@name", wfEntity.name));
            parameterList.Add(new SqlParameter("@pos", wfEntity.pos));
            parameterList.Add(new SqlParameter("@usever", wfEntity.usever));
            parameterList.Add(new SqlParameter("@createtime", wfEntity.createtime));
            parameterList.Add(new SqlParameter("@lasttime", wfEntity.lasttime));
            parameterList.Add(new SqlParameter("@creater_id", wfEntity.creater_id));
            parameterList.Add(new SqlParameter("@creater_name", wfEntity.creater_name));
            parameterList.Add(new SqlParameter("@lastuser_id", wfEntity.lastuser_id));
            parameterList.Add(new SqlParameter("@lastuser_name", wfEntity.lastuser_name));
            parameterList.Add(new SqlParameter("@users", wfEntity.users));
            parameterList.Add(new SqlParameter("@depts", wfEntity.depts));
            parameterList.Add(new SqlParameter("@status", wfEntity.status));
            parameterList.Add(new SqlParameter("@descn", wfEntity.descn));
            parameterList.Add(new SqlParameter("@wf_fqrids", wfEntity.wf_fqrids));
            parameterList.Add(new SqlParameter("@wf_dxbz", wfEntity.wf_dxbz));
            string sql = " insert into wf_wf(wftemplate_id,form_id,wftype_id,name,pos,usever,createtime,lasttime,creater_id,creater_name," +
                " lastuser_id,lastuser_name,users,depts,status,descn,wf_fqrids,wf_dxbz ) " +
                " VALUES(@wftemplate_id,@form_id,@wftype_id,@name,@pos,@usever,@createtime,@lasttime,@creater_id,@creater_name,@lastuser_id," +
                " @lastuser_name,@users,@depts,@status,@descn,@wf_fqrids,@wf_dxbz);select @@identity";
            string id = DBExecuteUtil.getUniqueResult(sql, parameterList.ToArray());
            return int.Parse(id);
        }

        public void DleteEntity(int wf_id)
        {
            List<SqlParameter> parameterList = new List<SqlParameter>();
            parameterList.Add(new SqlParameter("@wf_id", wf_id));
            string sql = "delete from wf_wf where wf_id=@wf_id ";
            DBExecuteUtil.executeSqlWithPar(sql, parameterList.ToArray());
        }

        public WfEntity QueryEntity(string wf_id)
        {
            WfEntity wfEntity = null;
            List<SqlParameter> parameterList = new List<SqlParameter>();
            parameterList.Add(new SqlParameter("@wf_id", wf_id));
            string sql = "select wf_id,form_id,wftemplate_id,name,pos,usever,createtime,lasttime,creater_id,creater_name," +
                " lastuser_id,lastuser_name,users,depts,status,descn,wf_fqrids,wf_dxbz from wf_wf where wf_id=@wf_id";

            var result = DBExecuteUtil.querySqlArray(sql, parameterList.ToArray());
            if (null == result) return null;

            wfEntity = new WfEntity();
            wfEntity.wf_id = int.Parse(result[0][0]);
            wfEntity.form_id = int.Parse(result[0][1]);
            wfEntity.wftemplate_id = int.Parse(result[0][2]);
            wfEntity.name = result[0][3];
            wfEntity.pos = int.Parse(result[0][4]);
            wfEntity.usever = int.Parse(result[0][5]);
            wfEntity.createtime = result[0][6];
            wfEntity.lasttime = result[0][7];
            wfEntity.creater_id = int.Parse(result[0][8]);
            wfEntity.creater_name = result[0][9];
            wfEntity.lastuser_id = int.Parse(result[0][10]);
            wfEntity.lastuser_name = result[0][11];
            wfEntity.users = result[0][12];
            wfEntity.depts = result[0][13];
            wfEntity.status = int.Parse(result[0][14]);
            wfEntity.descn = result[0][15];
            wfEntity.wf_fqrids = result[0][16];
            wfEntity.wf_dxbz = int.Parse(result[0][17]);
            return wfEntity;
        }

        public void UpdateEntity(WfEntity wfEntity)
        {
            List<SqlParameter> parameterList = new List<SqlParameter>();
            parameterList.Add(new SqlParameter("@wf_id", wfEntity.wf_id));
            parameterList.Add(new SqlParameter("@form_id", wfEntity.form_id));
            parameterList.Add(new SqlParameter("@wftemplate_id", wfEntity.wftemplate_id));
            parameterList.Add(new SqlParameter("@name", wfEntity.name));
            parameterList.Add(new SqlParameter("@pos", wfEntity.pos));
            //parameterList.Add(new SqlParameter("@ver", wfEntity.ver));
            parameterList.Add(new SqlParameter("@usever", wfEntity.usever));
            //parameterList.Add(new SqlParameter("@catalogy", wfEntity.catalogy));
            parameterList.Add(new SqlParameter("@createtime", wfEntity.createtime));
            parameterList.Add(new SqlParameter("@lasttime", wfEntity.lasttime));
            parameterList.Add(new SqlParameter("@creater_id", wfEntity.creater_id));
            parameterList.Add(new SqlParameter("@creater_name", wfEntity.creater_name));
            parameterList.Add(new SqlParameter("@lastuser_id", wfEntity.lastuser_id));
            parameterList.Add(new SqlParameter("@lastuser_name", wfEntity.lastuser_name));
            parameterList.Add(new SqlParameter("@users", wfEntity.users));
            parameterList.Add(new SqlParameter("@depts", wfEntity.depts));
            parameterList.Add(new SqlParameter("@status", wfEntity.status));
            parameterList.Add(new SqlParameter("@descn", wfEntity.descn));
            parameterList.Add(new SqlParameter("@wf_fqrids", wfEntity.wf_fqrids));
            parameterList.Add(new SqlParameter("@wf_dxbz", wfEntity.wf_dxbz));
            string sql = "update wf_wf set form_id=@form_id,wftemplate_id=@wftemplate_id,name=@name,pos=@pos,usever=@usever,createtime=@createtime,lasttime=@lasttime,creater_id=@creater_id,creater_name=@creater_name," +
                " lastuser_id=@lastuser_id,lastuser_name=@lastuser_name,users=@users,depts=@depts,status=@status,descn=@descn,wf_fqrids=@wf_fqrids,wf_dxbz=@wf_dxbz where wf_id=@wf_id";

            DBExecuteUtil.executeSqlWithPar(sql, parameterList.ToArray());
        }

        public WfEntity QueryEntityByuniqueid(string uniqueid)
        {
            WfEntity wfEntity = null;
            List<SqlParameter> parameterList = new List<SqlParameter>();
            parameterList.Add(new SqlParameter("@uniqueid", uniqueid));
            string sql = "select wf.wf_id,form_id,wftemplate_id,name,pos,usever,createtime,lasttime,creater_id,creater_name," +
                " lastuser_id,lastuser_name,users,depts,status,descn,wf_fqrids,wf_dxbz from wf_wf  wf,wf_ins ins where wf.wf_id=ins.wf_id and ins.uniqueid=@uniqueid ";

            var result = DBExecuteUtil.querySqlArray(sql, parameterList.ToArray());
            if (null == result) return null;

            wfEntity = new WfEntity();
            wfEntity.wf_id = int.Parse(result[0][0]);
            wfEntity.form_id = int.Parse(result[0][1]);
            wfEntity.wftemplate_id = int.Parse(result[0][2]);
            wfEntity.name = result[0][3];
            wfEntity.pos = int.Parse(result[0][4]);
            wfEntity.usever = int.Parse(result[0][5]);
            wfEntity.createtime = result[0][6];
            wfEntity.lasttime = result[0][7];
            wfEntity.creater_id = int.Parse(result[0][8]);
            wfEntity.creater_name = result[0][9];
            wfEntity.lastuser_id = int.Parse(result[0][10]);
            wfEntity.lastuser_name = result[0][11];
            wfEntity.users = result[0][12];
            wfEntity.depts = result[0][13];
            wfEntity.status = int.Parse(result[0][14]);
            wfEntity.descn = result[0][15];
            wfEntity.wf_fqrids = result[0][16];
            wfEntity.wf_dxbz = int.Parse(result[0][17]);
            return wfEntity;
        }

        public DataTable QueryAllWf()
        {
            //List<SqlParameter> parameterList = new List<SqlParameter>();
            //parameterList.Add(new SqlParameter("@user_id", user_id));
            string sql = " select wf_id 编码,name 名称,createtime 创建时间,creater_name 创建人 from wf_wf  ";

            DataTable dt = DBExecuteUtil.querySqlTable(sql);
            return dt;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="status">1:true,0:false</param>
        /// <returns></returns>
        public DataTable QueryAllWf(int status)
        {
            List<SqlParameter> parameterList = new List<SqlParameter>();
            parameterList.Add(new SqlParameter("@status", status));
            string sql = " select wf_id 编码,name 名称,createtime 创建时间,creater_name 创建人,wftype_id 类型编码,wftemplate_id 模板编码,wf_fqrids 发起人组  from wf_wf where status=@status ";

            DataTable dt = DBExecuteUtil.querySqlTable(sql, parameterList.ToArray());
            return dt;
        }

        public DataTable QueryByType(string wftype_name)
        {
            List<SqlParameter> parameterList = new List<SqlParameter>();
            parameterList.Add(new SqlParameter("@wftype_name", wftype_name));
            string sql = " select wf_id 编码,name 名称,createtime 创建时间,creater_name 创建人 from wf_wf wf,wf_wftype wftype "
                             + " where wf.wftype_id=wftype.wftype_id and wftype_name=@wftype_name ";

            DataTable dt = DBExecuteUtil.querySqlTable(sql, parameterList.ToArray());
            return dt;
        }

        #endregion

        #region 实例值操作
        public void WfinsvalueAdd(List<WfInsvalueEntity> wivList, int task_id, Guid insguid)
        {
            string str = "";
            foreach (WfInsvalueEntity item in wivList)
            {
                str += "insert into wf_insvalue(task_id,fieldname,fieldvalue,fieldtype) " +
                    "values(" + task_id + "," + item.fieldName + "," + item.fieldValue + "," + item.fieldType + ");";
            }
            DBExecuteUtil.executeSql(str);
        }
        #endregion
    }
}
